﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2010
// admin@franknagl.de
//
namespace SBIP.Filter
{
    using System;

    /// <summary>
    /// Gaussian blur filter with a fixed kernel.
    /// </summary>
    /// <remarks>
    /// <para>The filter implements Gaussian operator, which calculates each pixel
    /// of the result image as weighted sum of the correspond pixel and its neighbors in the source
    /// image. The weights are set by a fixed convolution kernel. The weighted
    /// sum is divided by the divisor before putting it into result image.
    /// </para>
    /// <para>Therefore two versions of kernels exists:</para>
    /// <code lang="none">
    ///      V1                V2
    /// 1  2  2  2  1     2  4  5  4  2
    /// 2  4  6  4  2     4  9 12  9  4
    /// 2  6  7  6  2     5 12 15 12  5
    /// 2  4  6  4  2     4  9 12  9  4
    /// 1  2  2  2  1     2  4  5  4  2
    /// </code>
    /// 
    /// <para>Sample usage:</para>
    /// <code>
    /// Bitmap image = new Bitmap("cathedral.jpg");
    ///
    /// // 1. Offline rendering of images
    /// // create Processor, used as rendering framework
    /// Processor processor = new Processor( );
    /// // starts Processor
    /// processor.Begin( image );
    /// // create FixedGaussianBlur filter
    /// FixedGaussianBlur filter = new FixedGaussianBlur( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    ///    filter.Version = FixedGaussianBlur.Versions.Strong;
    /// // apply the filter, render to bitmap  
    /// Bitmap resultImage = processor.RenderToBitmap( );
    /// resultImage.Save( "FixedGaussianBlur.jpg", ImageFormat.Jpeg );
    /// // apply the filter, render to texture
    /// // Texture2D resultTexture = processor.RenderToTexture( );
    /// processor.End( );
    ///  
    /// // 2. Online rendering of videos or images
    /// // create any windows control for rendering in
    /// Form myForm = new Form
    /// {
    ///     Width = 1280,
    ///     Height = 720
    /// };
    /// // starts Processor
    /// processor.Begin( "Video.avi", myForm );
    /// // create FixedGaussianBlur filter
    /// filter = new FixedGaussianBlur( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// filter.Version = FixedGaussianBlur.Versions.Light;
    /// // apply the filter
    /// myForm.Show( );
    /// while ( myForm.Created )
    /// {
    ///     processor.Render( );
    ///     Application.DoEvents( );
    /// }
    /// processor.End( );
    /// </code>
    /// 
    /// <para><b>Initial image:</b></para>
    /// <img src="../../Cathedral.jpg" width="500" height="338" />
    /// <para><b>Result image:</b></para>
    /// <img src="../../FixedGaussianBlur.jpg" width="500" height="338" />
    /// </remarks>
    public sealed class FixedGaussianBlur : BaseFilter
    {
        /// <summary>Two different versions of Gaussian blur filter.</summary>
        public enum Versions
        {
            /// <summary>
            /// Kernel with a divisor of 71.
            /// Kernel matrix:    
            /// <code lang="none">
            /// 1  2  2  2  1
            /// 2  4  5  4  2
            /// 2  5  7  5  2
            /// 2  4  5  4  2 
            /// 1  2  2  2  1
            /// </code>
            /// </summary>
            Light,
            /// <summary>
            /// Kernel with a divisor of 71.
            /// Kernel matrix:    
            /// <code lang="none">
            /// 2  4  5  4  2
            /// 4  9 12  9  4
            /// 5 12 15 12  5
            /// 4  9 12  9  4
            /// 2  4  5  4  2
            /// </code>
            /// </summary>
            Strong
        }

        #region effect properties
        private Versions version;
        /// <summary>The version of Gaussian blur filter.</summary>
        public Versions Version
        {
            get { return version; }
            set
            {
                if (Effect == null)
                    throw new ArgumentException("Version" + InitMsg);

                version = value;
                Effect.Parameters["Version"].SetValue((int)Version);
            }
        }
        #endregion effect properties

        /// <summary>
        /// Initializes a new instance of the <see cref="FixedGaussianBlur"/> class.
        /// </summary>
        public FixedGaussianBlur() : base("FixedGaussianBlur") { }
    }
}
